﻿<?php
class Model extends CI_Model {

	private $niveauAuthorisationUtilisateur = 1; //permet de connaitre le niveau de l'utilisateur

	function __construct() {
		parent::__construct();
	}

    /*
      EXEMPLE :

      function create($message){
      $this->db->insert('message',$message);
      }

      function commentaire($commentaire){
      $this->db->query("SELECT * FROM message WHERE id = {$commentaire['fk_id_message']} AND id NOT IN (SELECT fk_id_message FROM commentaire)");

      if($this->db->count_all_results() == 1){
      $this->db->insert('commentaire',$commentaire);
      }
      }
     */

    /* ########################################################################
      ZONE TRANSVERSALE
      ########################################################################## */

		//read tous les lvl dispo en BDD
      public function readSchoolAllLvl(){
      	$query = $this->db->get('REF_SCHOOL_LEVEL_LVL');
      	return $query->result();
      }

		//read toutes les cat dispo en BDD
		public function readCat(){
			$query = $this->db->get('ref_category_cat');
			return $query->result();
		}
		
		//read toutes les school dispo en BDD
		public function readSchool(){
			$query = $this->db->get('tm_school_schl');
			return $query->result();
		}
		
		public function readDisabledUser(){
			$query = $this->db->query("SELECT * FROM tm_user_usr where USR_account_valid = 0 OR USR_account_valid = false");
			return $query->result();
		}
		
		//recup l'id de school from le groupe
      public function getShoolFromGroup($id){
      	$query = "SELECT FK_SCHL FROM tm_group_grp WHERE PK_GRP = {$id}";
      	$query = $this->db->query($query);
      	$query = $query->result();
      	return $query[0]->FK_SCHL;
      }

	//permet de valider un Login
	  private function checkLogin($login,$pwd){
			$validation = false;
			
			$query = $this->db->get('tm_user_usr');
			$query = "SELECT * FROM tm_user_usr WHERE USR_account_valid = 1";
			$query = $this->db->query($query);
			$resultat = $query->result();
			
			foreach($resultat as $row){				
				
				if($row->USR_PWD == $pwd && strtoupper($row->USR_email) == strtoupper($login)){

					$validation = true; // l'user ࡬es droit d'acc鳊
					//modifier la var globale ==> $niveauAuthorisationUtilisateur
					break;
				}
			}
			return $validation;
		}

    //permet de v�rifier un utilisateur
		public function checkCookie(){
			$toReturn = false;

			if(get_cookie('login_ndpi',TRUE) && get_cookie('pwd_ndpi',TRUE)){
				if($this->checkLogin(get_cookie('login_ndpi','value'),get_cookie('pwd_ndpi','value'))){
					$_SESSION['login'] = get_cookie('login_ndpi','value');
					$_SESSION['pwd'] = get_cookie('pwd_ndpi','value');
					$toReturn = true;
				}else{
					delete_cookie("login_ndpi");
					delete_cookie("pwd_ndpi");
					if(isset($_SESSION)) session_destroy();
				}
			}else{
				if(isset($_SESSION)) session_destroy();
			}
			return $toReturn;
		}

    //permet de connecter un utilisateur
		function connectUser($login, $pwd) {
        // $pwdCrypt = sha1(md5(crypt($pwd, $this->PhraseDeCryptage)));
			$pwdCrypt = $pwd;

			if ($this->checkLogin($login, $pwdCrypt)) {
				$cookieLog = array(
					'name' => 'login_ndpi',
					'value' => $login,
					'expire' => '86500',
					'domain' => '',
					'path' => '/',
					'prefix' => '',
					);
				$cookiePwd = array(
					'name' => 'pwd_ndpi',
					'value' => $pwdCrypt,
					'expire' => '86500',
					'domain' => '',
					'path' => '/',
					'prefix' => '',
					);

				set_cookie($cookieLog);
				set_cookie($cookiePwd);
				$_SESSION['login'] = $cookieLog;
				$_SESSION['pwd'] = $cookiePwd;
			} else {
				header('Location: ' . base_url());
			}
		}

    //Permet de deco un utilisateur et de le rediriger proprement
		function logoutUser() {
			delete_cookie("login_ndpi");
			delete_cookie("pwd_ndpi");
			if(isset($_SESSION)) session_destroy();
			header('Location: ' . base_url());
		}

    /* ########################################################################
      FIN ZONE TRANSVERSALE
      ########################################################################## */


    /* ########################################################################
      ZONE UTILISATEURS /  PARTICIPANTS
      ########################################################################## */

    //Lire les utilisateurs
      function ReadUsers() {

      	$query = $this->db->get('tm_user_usr');


      	return $query->result();
      }


    //Lecture de la todoList
      function ReadTodo($utilisateur = ''){
      	if($utilisateur == ''){

      	}else{
      		$query = $this->db->join('tm_user_usr', 'tm_task_tsk.FK_USR = tm_user_usr.PK_USR');
      		$query = $this->db->join('ref_item_itm', 'tm_task_tsk.FK_ITM = ref_item_itm.PK_ITM');
      		$query = $this->db->where('tm_user_usr.USR_email',$utilisateur);
      		$query = $this->db->get('tm_task_tsk');
      	}



      	return $query->result();
      }

    //CRUD Tache
    //Lire les tâches
    // Si on passe un user en param, on fait la jointure
      function ReadTache($utilisateur = '') {

      	if ($utilisateur == '') {
      		$query = $this->db->get('tm_task_tsk');
      	} else {
      		$this->db->from('tm_task_tsk');
      		$query = $this->db->join('tm_user_usr', 'tm_task_tsk.FK_USR = tm_user_usr.PK_USR');
      		$query = $this->db->where('tm_user_usr.PK_USR');
      		$query = $this->db->get('');
      	}

      	return $query->result();
      }



    //lire une tache
      function ReadOneTache($id) {
      	$query = $this->db->where('PK_TSK',$id);
      	$query = $this->db->join("ref_item_itm", 'ref_item_itm.PK_ITM = tm_task_tsk.FK_ITM');
      	$query = $this->db->join("tm_user_usr", "tm_user_usr.PK_USR = tm_task_tsk.FK_USR");
      	$query = $this->db->get('tm_task_tsk');
      	return $query->result();
      }

    //Créer une tâche
      function CreateTache($utilisateur = '') {
      	if ($utilisateur == '') {

      	} else {
      		$query = $this->db->join('tm_user_usr', 'tm_task_tsk.FK_USR = tm_user_usr.PK_USR');
      		$query = $this->db->where('tm_user_usr.PK_USR');
      	}
      	$this->db->insert('tm_task_tsk', $utilisateur);
      }

    //modifie les tâches
      function UpdateTache($uneTache) {
      	$this->db->update("tm_task_tsk", $uneTache);
      }

    //Supprime une tâche
      function DeleteTache($utilisateur = '') {
//        $id = $utilisateur['PK_USR'];
      	if ($utilisateur == '') {

      	} else {
      		$this->db->where('PK_TSK', $utilisateur);

      	}
      	$this->db->delete('tm_task_tsk');
      }

    //Fin CRUD Tache

      public function readUserByLibelle($libelle) {
      	$query = $this->db->where("tm_user_usr.USR_firstname", $libelle);
      	$query = $this->db->get("tm_user_usr");

      	return $query->result();
      }
      public function readItemByLibelle($libelle) {
      	$query = $this->db->where("ITM_LIB", $libelle);
      	$query = $this->db->get("ref_item_itm");

      	return $query->result();
      }

    // renvoi le groupe d'un user
      public function readGroupUser($mailUser)
      {
      	$query = $this->db->join('tm_group_grp', 'tm_group_grp.PK_GRP = tm_user_usr.FK_GRP');
      	$query = $this->db->where("tm_user_usr.USR_email", $mailUser);
      	$query = $this->db->get("tm_user_usr");

      	return $query->result();
      }

    /* ########################################################################
      FIN ZONE UTILISATEURS / PARTICIPANTS
      ########################################################################## */


    /* ########################################################################
      ZONE JURY
      ########################################################################## */
      // fonction pour lire les groupes
      public function readAllGroup() {
      	$query = $this->db->get('tm_group_grp');
      	return $query->result();
      }

      public function readOneGroup($group) {
      	$query = $this->db->where("tm_group_grp.PK_GRP", $group);
      	$query = $this->db->get("tm_group_grp");

      	return $query->result();
      }

      public function ReadItem(){
      	$query = $this->db->get('ref_item_itm');
      	return $query->result(); 
      }

      public function readAllCategory() {

      	$query = $this->db->get("ref_category_cat");
      	return $query->result();
      }

      public function readAllItem() {
      	$query = $this->db->select("");
      	$query = $this->db->get("ref_item_itm");
      	return $query->result();
      }

      public function readTaskPerGroup($group) {
        $query = $this->db->select("");
        $query = $this->db->join('tm_user_usr', 'tm_task_tsk.FK_USR = tm_user_usr.PK_USR');
        $query = $this->db->join('tm_group_grp', 'tm_user_usr.FK_GRP = tm_group_grp.PK_GRP');
        $query = $this->db->where("tm_group_grp.PK_GRP", $group);
        $query = $this->db->get("tm_task_tsk");
        return $query->result();
      }


    /* ########################################################################
      FIN ADMIN JURY
      ########################################################################## */


    /* ########################################################################
      ZONE ADMIN
      ########################################################################## */
      
      //Va récuperer le nomp des participants et leurs groupes 
      public function ReadUserGroupe(){
         
        $query = $this->db->join('tm_group_grp', 'tm_user_usr.FK_GRP = tm_group_grp.PK_GRP');
        $query = $this->db->join('tm_school_schl', 'tm_user_usr.FK_SCHL = tm_school_schl.PK_SCHL');
        
//      $query = $this->db->where('tm_user_usr.USR_account_valid',false);
        $query = $this->db->get('tm_user_usr');
        return $query->result();
        
      }
    /* ########################################################################
      FIN ZONE ADMIN
      ########################################################################## */
  }

  ?>